'''
Created on Jun 4, 2010

@author: roni
'''
from generators.environmentGenerator import EnvironmentGenerator
import logging
from projectConstants import ProjectConstants
from diagnosisEnvironment import DiagnosisEnvironment
from experimentRunner import ExperimentRunner
import os

def try_to_diagnose(loaded_environment):
    # Test diagnosis
    logging.info("Asserting that loaded environment can be diagnosed")
    diagnosis_runner = ExperimentRunner()
    diagnosis_runner.lp_diagnose(loaded_environment.scenario, 
                                 loaded_environment.observations.flow_to_details,
                                 loaded_environment.added_delays)
    diagnosis_runner.gde_diagnose(loaded_environment.scenario, 
                                 loaded_environment.observations.flow_to_details,
                                 loaded_environment.added_delays)     

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    #node_range = [100,400,600,800,1000]
    node_range = xrange(900,99,-200)
    added_edges_range = [2]    
    agents_range = [5,15,50,60,70]
    #agents_range = xrange(3,19,2)
    #normal_probability_steps_range = [1,6]#xrange(6,7)
    normal_probability_steps_range = [19,16,14,10] 
    iterations = xrange(25)   

      
    environment_dir = "%s/environments" % ProjectConstants.RESROUCE_DIR
    generator = EnvironmentGenerator()    
    for nodes in node_range:
            for added_edges in added_edges_range:
                for agents in agents_range:
                    for normal_probability_step in normal_probability_steps_range:
                        normal_probability=normal_probability_step*0.05
                        for iteration in iterations:
                            file_prefix = "Env-%s.%s.%s.%3.2f.%s" % (nodes, added_edges, agents, normal_probability, iteration)
                            full_file_prefix = "%s/%s" % (environment_dir,file_prefix)
                            
                            if os.path.exists("%s.scenario" % full_file_prefix)==True:
                                logging.info("Environment %s already created" % file_prefix)
#                                loaded_environment = DiagnosisEnvironment(full_file_prefix)
#                                try_to_diagnose(loaded_environment)
                                continue
                            logging.info("Generating %s" % file_prefix)
                            environment = generator.generate(nodes, added_edges, agents, normal_probability, iteration)                            
                            environment.save(full_file_prefix)
                            
                            # Assertions
#                            logging.info("Asserting that loaded environment is the same")
#                            loaded_environment = DiagnosisEnvironment(full_file_prefix)
#                            if environment.is_equal(loaded_environment)==False:
#                                raise ValueError("save/load generated different environments")
                            
                            # Test diagnosis
#                            try_to_diagnose(loaded_environment)                          
                            